package id.co.shuba.spamobile.util;

import java.io.IOException;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

public class XmlUtil {	
	public static String ns = null;
	
	public static long readLong(XmlPullParser parser, String tag) throws IOException, XmlPullParserException {
		parser.require(XmlPullParser.START_TAG, ns, tag);
        String text = readText(parser);
        long val = 0;
        try {
			val = Long.parseLong(text);
		} catch (Exception e) {		
		}        
        parser.require(XmlPullParser.END_TAG, ns, tag);
        return val;
    }
	
	public static int readInt(XmlPullParser parser, String tag) throws IOException, XmlPullParserException {
        parser.require(XmlPullParser.START_TAG, ns, tag);
        String text = readText(parser);
        int val = 0;
        try {
			val = Integer.parseInt(text);
		} catch (Exception e) {		
		}        
        parser.require(XmlPullParser.END_TAG, ns, tag);
        return val;
    }
	
	public static String readString(XmlPullParser parser, String tag) throws IOException, XmlPullParserException {
        parser.require(XmlPullParser.START_TAG, ns, tag);
        String val = readText(parser);
        parser.require(XmlPullParser.END_TAG, ns, tag);
        return val;
    }
	
	public static String readText(XmlPullParser parser) throws IOException, XmlPullParserException {
        String result = "";
        if (parser.next() == XmlPullParser.TEXT) {
            result = parser.getText();
            parser.nextTag();
        }
        return result;
    }
	
	public static void skip(XmlPullParser parser) throws XmlPullParserException, IOException {
        if (parser.getEventType() != XmlPullParser.START_TAG) {
            throw new IllegalStateException();
        }
        int depth = 1;
        while (depth != 0) {
            switch (parser.next()) {
            case XmlPullParser.END_TAG:
                    depth--;
                    break;
            case XmlPullParser.START_TAG:
                    depth++;
                    break;
            }
        }
    }
}
